package app

import (
	"gina/app/common/request"
	"gina/app/common/response"
	"gina/app/services"
	"github.com/gin-gonic/gin"
)

// @Summary 用户登录
// @Description 用户登录接口，通过用户名和密码进行登录
// @Tags auth
// @Accept json
// @Produce json
// @Param login body request.Login true "登录信息"
// @Success 200 {object} response.Response"登录成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 401 {object} response.Response "用户名或密码错误"
// @Failure 500 {object} response.Response "服务器内部错误"
// @Router /auth/login [post]
func Login(c *gin.Context) {
	var form request.Login
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	if err, user := services.UserService.Login(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		tokenData, err, _ := services.JwtService.CreateToken(services.AppGuardName, user)
		if err != nil {
			response.BusinessFail(c, err.Error())
			return
		}
		response.Success(c, tokenData)
	}
}
